﻿using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Drawing; 
using System.ComponentModel; 
using System.Data; 
using System.Windows.Forms; 
using System.IO; 
using System.Linq; 
using System.Globalization; 
using System.Runtime.Serialization.Formatters.Binary; 
using System.Text; 

using EAS.Data; 
using EAS.Data.ORM; 
using EAS.Data.Access; 
using EAS.Modularization; 

using EAS.Services; 
using EAS.Data.Linq; 

using HR.Entities; 

namespace HR.WinUI 
{ 
     /// <summary> 
     /// 员工列表。 
     /// </summary> 
     public partial class employeeEditor : Form 
     { 
  
         public employeeEditor() 
         { 
             InitializeComponent(); 
         } 
         
         #region  DataEntity
         
         public employee DataEntity 
         { 
             get 
             { 
                return this.m_DataEntity; 
             } 
             set 
             { 
                if(value == null) 
                    return; 
                this.m_DataEntity = value; 
                this.RefreshData(); 
             } 
         } 

         employee m_DataEntity; 
         
         #endregion
         
         void RefreshData() 
         { 
             if(this.DataEntity != null) 
             { 
                this.tbName.Text=this.m_DataEntity.Name; 
                this.tbWorkshop .Text=this.m_DataEntity.Workshop ; 
                this.tbSex.Text=this.m_DataEntity.Sex; 
                this.tbBirthdate.Text=this.m_DataEntity.Birthdate.ToString(); 
                this.tbInputCode.Text=this.m_DataEntity.InputCode; 
             } 
         } 
        
        bool ValidInput() 
        { 
            if ( this.tbName.Text==string.Empty ) 
            { 
                MessageBox.Show(this, "员工名字 ： 必填项,不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); 
                this.tbName.Focus(); 
                return false; 
            } 
            if ( this.tbWorkshop .Text==string.Empty ) 
            { 
                MessageBox.Show(this, "车间班组 ： 必填项,不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); 
                this.tbWorkshop .Focus(); 
                return false; 
            } 
            if ( this.tbSex.Text==string.Empty ) 
            { 
                MessageBox.Show(this, "性别 ： 必填项,不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); 
                this.tbSex.Focus(); 
                return false; 
            } 
            if ( this.tbBirthdate.Text==string.Empty ) 
            { 
                MessageBox.Show(this, "出生日期 ： 必填项,不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); 
                this.tbBirthdate.Focus(); 
                return false; 
            } 
            if ( this.tbInputCode.Text==string.Empty ) 
            { 
                MessageBox.Show(this, "输入码 ： 必填项,不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); 
                this.tbInputCode.Focus(); 
                return false; 
            } 
            return true; 
        } 
        private void btnSave_Click(object sender, EventArgs e) 
        { 
            if (!this.ValidInput()) 
            { 
                return; 
            } 
 
            this.Cursor = System.Windows.Forms.Cursors.WaitCursor; 
 
            try 
            { 
                employee dataEntity = null; 
 
                if (this.DataEntity == null) 
                { 
                    dataEntity = new employee(); 
                } 
                else 
                { 
                    dataEntity = this.DataEntity; 
                } 
 
                //基本信息 
                dataEntity.Name=this.tbName.Text; 
                dataEntity.Workshop =this.tbWorkshop .Text; 
                dataEntity.Sex=this.tbSex.Text; 
                dataEntity.Birthdate=this.tbBirthdate.Text!=null? Convert.ToDateTime(this.tbBirthdate.Text) : DateTime.Now; 
                dataEntity.InputCode=this.tbInputCode.Text;
                dataEntity.LMTime = DateTime.Now;
                if (this.DataEntity == null) 
                { 
                    dataEntity.Insert(); 
                } 
                else 
                { 
                    dataEntity.Update(); 
                } 
 
                this.m_DataEntity = dataEntity; 
                
                this.DialogResult = DialogResult.OK;
                this.Close();
            } 
            catch (System.Exception ex) 
            { 
                MessageBox.Show(this, "在保存‘员工列表’数据的过程中发生了一个错误： " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); 
            } 
            finally 
            { 
                this.Cursor = System.Windows.Forms.Cursors.Default; 
            } 
        } 
        
        private void btnClose_Click(object sender, EventArgs e) 
        { 
             this.Close();  
        } 
        
   } 
} 
